package erlwitch.gui;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import javax.swing.JLabel;
import javax.swing.JTextField;

import org.gjt.sp.jedit.AbstractOptionPane;

import erlwitch.ErlwitchPlugin;

public class ErlwitchOptions extends AbstractOptionPane {

	public class ErlwitchConfig implements Serializable {
		private static final long serialVersionUID = 1L;
		String metadataRoot = ".";
		String findDefinitionShortcut = "A+e A+f";
		String backShortcut = "A+e A+b";
		String forwardShortcut = "A+e A+v";
		String compileShortcut = "A+e A+c";
		String findNext = "F3";
		String findPrevious = "S+F3";
	}
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private JTextField metadataRoot;
	private JTextField findDefinitionShortcut;
	private JTextField backShortcut;
	private JTextField forwardShortcut;
	private JTextField compileShortcut;
	private JTextField findNext;
	private JTextField findPrevious;
	private ErlwitchConfig configData;

	private File configFile;
	
	/**
	 * @author Luis Chiruzzo
	 */
	public ErlwitchOptions(String name) {
		super(name);
	}
	
	public void _init() {
		// String[] stackChoices = new String[] {
		// jEdit.getProperty("navigator.back.label"),
		// jEdit.getProperty("navigator.forward.label") };
		addComponent(new JLabel("<html><h3>Erlwitch</h3>"));
		try {
			configFile = new File(ErlwitchPlugin.getRootDir(), ErlwitchPlugin.CONFIG_FILE);
			if (!configFile.exists()) {
				configFile.createNewFile();
				configData = new ErlwitchConfig();
				ObjectOutputStream w = new ObjectOutputStream(new FileOutputStream(configFile));
				w.writeObject(configData);
				w.close();
			} else {
				ObjectInputStream r = new ObjectInputStream(new FileInputStream(configFile));
				configData = (ErlwitchConfig)r.readObject();
			}
			
			addSeparator("Metadata");
			metadataRoot = new JTextField(configData.metadataRoot, 20);
			addComponent(new JLabel("Metadata Root"), metadataRoot);
			addSeparator("Shortcuts");
			findDefinitionShortcut = new JTextField(configData.findDefinitionShortcut, 5);
			addComponent(new JLabel("Find definition shortcut"), findDefinitionShortcut);
			backShortcut = new JTextField(configData.backShortcut, 5);
			addComponent(new JLabel("Back shortcut"), backShortcut);
			forwardShortcut = new JTextField(configData.forwardShortcut, 5);
			addComponent(new JLabel("Forward shortcut"), forwardShortcut);
			compileShortcut = new JTextField(configData.compileShortcut, 5);
			addComponent(new JLabel("Compile shortcut"), compileShortcut);
			findNext = new JTextField(configData.findNext, 5);
			addComponent(new JLabel("Find next shortcut"), findNext);
			findPrevious = new JTextField(configData.findPrevious, 5);
			addComponent(new JLabel("Find previous shortcut"), findPrevious);
			
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			addComponent(new JLabel("<html><h3>ERROR IMPOSIBLE!!!!</h3>"));
		} catch (IOException e) {
			e.printStackTrace();
			addComponent(new JLabel("<html><h3>ERROR!!!!</h3>"));
		}
	}

	public void _save()
	{
		configData.metadataRoot = metadataRoot.getText();
		configData.findDefinitionShortcut = findDefinitionShortcut.getText();
		configData.backShortcut = backShortcut.getText();
		configData.forwardShortcut = forwardShortcut.getText();
		configData.compileShortcut = compileShortcut.getText();
		configData.findNext = findNext.getText();
		configData.findPrevious = findPrevious.getText();
		
		try {
			ObjectOutputStream w = new ObjectOutputStream(new FileOutputStream(configFile));
			w.writeObject(configData);
			w.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//boolean useToolBars = showOnToolbar.isSelected();
		//jEdit.setBooleanProperty(getName() + ".showOnToolbar", useToolBars);
	}

	public String getName()
	{
		return "erlwitch";
	}
}
